//
//  WYMomentAttitudesItemViewModel.m
//  FriendCircleDemo
//
//  Created by WY on 2018/10/9.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "WYMomentAttitudesItemViewModel.h"

@interface WYMomentAttitudesItemViewModel ()
/// 单条说说
//@property (nonatomic, readwrite, strong) WYMoment *moment;

/// container
@property (nonatomic, readwrite, strong) YYTextContainer *container;

/// 富文本
@property (nonatomic, readwrite, strong) NSMutableAttributedString *attitudesAttr;

@end

@implementation WYMomentAttitudesItemViewModel

-(instancetype)initWithMoment:(WYMoment *)moment
{
    if (self = [super init]) {
        
        self.moment = moment;
        self.type = WYMomentContentTypeAttitude;
        
    }
    return self;
}

-(void)setMoment:(WYMoment *)moment
{
    _moment = moment;
    
    self.attitudesAttr = nil;
    UIFont *font = SYSTEMFONT(14.0);
    for (WYUser *user in moment.attitudesList) {
        NSMutableAttributedString *screenNameAttr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@，",user.screenName]];
        /// 设置昵称高亮
        NSRange range = NSMakeRange(0, user.screenName.length);
        [screenNameAttr setColor:MomentScreenNameTextColor range:range];
        [screenNameAttr setFont:font range:range];
        
        /// 点击的高亮的
        YYTextBorder *border = [YYTextBorder new];
        border.cornerRadius = 0;
        border.insets = UIEdgeInsetsMake(0, -1, 0, -1);
        border.fillColor = TextHighlightBackgroundColor;
        
        /// 点击高亮
        YYTextHighlight *highlight = [YYTextHighlight new];
        // 将用户数据带出去
        highlight.userInfo = @{WYMomentUserInfoKey:user};
        [highlight setBackgroundBorder:border];
        [screenNameAttr setTextHighlight:highlight range:range];
        
        /// 拼接
        [self.attitudesAttr appendAttributedString:screenNameAttr];
    }
    /// 去掉最后一个 ，
    [self.attitudesAttr deleteCharactersInRange:NSMakeRange(self.attitudesAttr.length-1, 1)];
    
    /// 统一配置
    self.attitudesAttr.lineBreakMode = NSLineBreakByCharWrapping;
    self.attitudesAttr.alignment = NSTextAlignmentLeft;
    
    /// 文本布局
    YYTextLayout *layout = [YYTextLayout layoutWithContainer:self.container text:self.attitudesAttr.copy];
    self.contentLableLayout = layout;
    self.contentLableFrame = CGRectMake(WYMomentCommentViewContentLeftOrRightInset, WYMomentCommentViewAttitudesTopOrBottomInset, layout.textBoundingSize.width, layout.textBoundingSize.height);
    self.cellHeight = layout.textBoundingSize.height + 2*WYMomentCommentViewAttitudesTopOrBottomInset;
}

#pragma mark - Getter

- (YYTextContainer *)container
{
    if (_container == nil) {
        CGFloat limitWidth = WYMomentCommentViewWidth()-2*WYMomentCommentViewContentLeftOrRightInset;
        _container = [YYTextContainer containerWithSize:CGSizeMake(limitWidth, MAXFLOAT)];
        _container.maximumNumberOfRows = 0;
    }
    return _container;
}

- (NSMutableAttributedString *)attitudesAttr
{
    if (_attitudesAttr == nil) {
        _attitudesAttr = [[NSMutableAttributedString alloc] init];
        UIFont *font = SYSTEMFONT(14.0);
        //// 默认拼接 “❤️  ”
        /// 爱心 ❤️
        UIImage *likeImage = [UIImage imageNamed:@"wx_albumInformationLikeHL_15x15"];
        likeImage = [UIImage imageWithCGImage:likeImage.CGImage scale:[UIScreen screenScale] orientation:UIImageOrientationUp];
        
        NSMutableAttributedString *attachLikeText = [NSMutableAttributedString attachmentStringWithContent:likeImage contentMode:UIViewContentModeCenter attachmentSize:likeImage.size alignToFont:font alignment:YYTextVerticalAlignmentCenter];
        [self.attitudesAttr appendAttributedString:attachLikeText];
        /// 拼接一个空格
        NSMutableAttributedString *marginAttr = [[NSMutableAttributedString alloc] initWithString:@"  "];
        [self.attitudesAttr appendAttributedString:marginAttr];
    }
    return _attitudesAttr;
}
@end
